﻿using Nasty.Core.LoadParams;
using Nasty.Core.Registry;
using Nasty.Core.Entity.System;
using Nasty.Core.SuperExtension;
using SqlSugar;

namespace Nasty.Core.Service
{
    /// <summary>
    /// 控制所有通知消息接口
    /// </summary>
    public interface INoticeService : IAutofacRegister
    {
        public ResultData<UserMessage> UserMessage(string userId, string title, string description, MessageType MessageType, string? extend);

        public ResultData<UserMessage> UserMessage(UserMessage msg);

        public ResultData<string> DeleteUserMessages(List<string> ids);
    }

    public class NoticeService : INoticeService
    {
        public required SqlSugarClient DbClient { get; set; }

        public ResultData<string> DeleteUserMessages(List<string> ids)
        {
            var result = new ResultData<string>();
            try
            {
                DbClient.Delete<UserMessage>(ids);
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

        ResultData<UserMessage> INoticeService.UserMessage(string userId, string title, string description, MessageType MessageType, string? extend)
        {
            var result = new ResultData<UserMessage>();

            try
            {
                var msg = new UserMessage
                {
                    UserId = userId,
                    Title = title,
                    Description = description,
                    MessageType = MessageType,
                    Extend = extend
                };

                DbClient.Add(msg);

                result.IsSuccess = true;
                result.Data = msg;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

        ResultData<UserMessage> INoticeService.UserMessage(UserMessage msg)
        {
            var result = new ResultData<UserMessage>();
            try
            {
                DbClient.Add(msg);
                result.IsSuccess = true;
                result.Data = msg;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }
    }
}
